#include "stdafx.h"
#include "LGUIMultiChoice.h"
#include "LGUIContainer.h"

LGUIMultiChoice::LGUIMultiChoice(int id, int _x, int _y, int _w, const TCHAR **strings, int _numChoices, int _userdata) : LGUIWindow(_x,_y,_w,12)
{
  ID=id;
  numChoices=_numChoices;
  userData=_userdata;
  selected=0;
  w=_w;
  text=new TCHAR *[_numChoices];
  for (int i=0; i<numChoices; i++)
  {
    text[i] = new TCHAR[_tcslen(strings[i])+1];
    _tcscpy(text[i],strings[i]);
  }
  h=ROWSIZE*numChoices+1;
}

LGUIMultiChoice::~LGUIMultiChoice()
{
}
void LGUIMultiChoice::penDown(int px, int py)
{
  if (isInside(px,py))
  {
    py-=y;
    py/=ROWSIZE;
    if (py<numChoices && py>=0 /*&& selected != py*/)
    {
      selected=py;
      parent->deliverMessage(LGUIMessage(MSG_KNOBCHANGED,ID,selected,userData));
    }
    redraw();
  }
}


void LGUIMultiChoice::draw(int xoff, int yoff)
{
  if (isDirty)
  {
    buffer.rect(x+xoff,y+yoff,x+xoff+w,y+yoff+h,COLOR(0x404040));
    for (int i=0; i<numChoices; i++)
    {
		i==selected ?
			buffer.rectFillDarkFaded(x+xoff+1,y+yoff+i*ROWSIZE+1,x+xoff+w,1+y+yoff+(i+1)*ROWSIZE, COLOR(0x407FFF)) : 
		buffer.rectFillFaded(x+xoff+1,y+yoff+i*ROWSIZE+1,x+xoff+w,1+y+yoff+(i+1)*ROWSIZE, COLOR(0xFFFFFF), COLOR(0xeFeFeF));
      if (text)
        if (text[i])
			buffer.drawText(text[i],x+xoff+4,y+yoff+i*ROWSIZE+4, i==selected ? 0xFFFF : 0x0000);
    }
    
    LRect dirty = *this;
    dirty.x+=xoff;
    dirty.y+=yoff;
    buffer.addDirtyRect(dirty);
    
    isDirty = false;
  }
  

}